package cn.harry.modular.achievement.controller;

import cn.harry.common.annotation.SysLog;
import cn.harry.common.api.R;
import cn.harry.common.enums.BusinessType;
import cn.harry.component.security.utils.SecurityUtils;
import cn.harry.modular.achievement.AchievementAuditVO;
import cn.harry.modular.achievement.enums.NoticeTypeEnums;
import cn.harry.modular.achievement.service.AchievementService;
import cn.harry.modular.achievement.domain.Achievement;
import cn.harry.modular.achievement.service.NoticeService;
import cn.harry.modular.system.domain.SysUser;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

/**
 * 成果资源前端控制层
 *
 * @author harry
 * @公众号 Harry技术
 */
@Tag(name = "成果资源接口")
@RestController
@RequestMapping("/achievement")
@RequiredArgsConstructor
public class AchievementController {

    private final AchievementService achievementService;
    private final NoticeService noticeService;

    /**
     * 分页查询列表
     */
    @Operation(summary = "分页查询")
    @GetMapping(value = "/page")
    public R<IPage<Achievement>> page(@ParameterObject Page<Achievement> page, @ParameterObject Achievement entity) {
        return R.success(achievementService.page(page, Wrappers.lambdaQuery(entity)));
    }

    /**
     * 根据id获取详情
     */
    @Operation(summary = "根据id获取详情")
    @GetMapping(value = "/{id}")
    public R<Achievement> getById(@PathVariable Long id) {
        return R.success(achievementService.getById(id));
    }

    /**
     * 新增
     */
    @Operation(summary = "新增")
    @SysLog(title = "achievement", businessType = BusinessType.INSERT)
    @PostMapping
    public R<Integer> save(@RequestBody Achievement entity) {

        long userId = SecurityUtils.getUserId();
        entity.setUploader(userId);
        entity.setUploadTime(DateUtil.date());

        return achievementService.save(entity) ? R.success() : R.failed();
    }

    /**
     * 更新
     */
    @Operation(summary = "更新")
    @SysLog(title = "achievement", businessType = BusinessType.UPDATE)
    @PutMapping
    public R<Integer> update(@RequestBody Achievement entity) {
        return achievementService.updateById(entity) ? R.success() : R.failed();
    }

    /**
     * 删除
     */
    @Operation(summary = "删除")
    @SysLog(title = "achievement", businessType = BusinessType.DELETE)
    @DeleteMapping(value = "/{ids}")
    public R<Integer> deleteByIds(@Parameter(description = "多个以英文逗号(,)拼接") @PathVariable Long[] ids) {
        return achievementService.removeBatchByIds(Arrays.asList(ids)) ? R.success() : R.failed();
    }

    @Operation(summary = "下载")
    @GetMapping(value = "/download/{id}")
    public R<Integer> download(@PathVariable Long id) {
        Achievement achievement = achievementService.getById(id);
        achievement.setDownloadCount(achievement.getDownloadCount() + 1);
        SysUser user = SecurityUtils.getSysUser();

        String content = StrUtil.format("《{}》被 {} 点击下载", achievement.getTitle(), user.getUsername());
        noticeService.saveInfo(user.getId(), content, NoticeTypeEnums.SYSTEM_NOTICE);

        return achievementService.updateById(achievement) ? R.success() : R.failed();
    }

    @Operation(summary = "审核")
    @SysLog(title = "achievement", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/audit")
    public R<Integer> audit(@RequestBody AchievementAuditVO vo) {

        return achievementService.audit(vo) ? R.success() : R.failed();

    }

    @Operation(summary = "获取我的成果")
    @GetMapping(value = "/my")
    public R<IPage<Achievement>> my(@ParameterObject Page<Achievement> page) {
        long userId = SecurityUtils.getUserId();
        return R.success(achievementService.page(page, Wrappers.lambdaQuery(Achievement.class).eq(Achievement::getUploader, userId)));
    }
}
